package club.zhaoliang.study.webservice;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.yanzhenjie.andserver.AndServer;
import com.yanzhenjie.andserver.Server;

import java.util.concurrent.TimeUnit;

import club.zhaoliang.study.R;

//https://yanzhenjie.com/AndServer
public class AndServerActivity extends AppCompatActivity {

    private static final String TAG = "AndServerActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_and_server);

        findViewById(R.id.tvStart).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //启动服务
                startServer();

            }
        });
    }

    public void startServer() {

        Server server = AndServer.webServer(this)
                .port(8080)
                .timeout(10, TimeUnit.SECONDS)
                .build();

        server.startup();

        if (server.isRunning()) {
            Log.e(TAG, "启动成功");
        } else {
            Log.e(TAG, "启动失败");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
